package com.delong.lease.web.app.service.impl;

import com.delong.lease.common.exception.LeaseException;
import com.delong.lease.common.result.ResultCodeEnum;
import com.delong.lease.model.entity.ApartmentInfo;
import com.delong.lease.model.entity.GraphInfo;
import com.delong.lease.model.entity.LabelInfo;
import com.delong.lease.model.entity.ViewAppointment;
import com.delong.lease.model.enums.ItemType;
import com.delong.lease.web.app.mapper.GraphInfoMapper;
import com.delong.lease.web.app.mapper.LabelInfoMapper;
import com.delong.lease.web.app.mapper.RoomInfoMapper;
import com.delong.lease.web.app.mapper.ViewAppointmentMapper;
import com.delong.lease.web.app.service.ApartmentInfoService;
import com.delong.lease.web.app.service.ViewAppointmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.delong.lease.web.app.vo.apartment.ApartmentItemVo;
import com.delong.lease.web.app.vo.appointment.AppointmentDetailVo;
import com.delong.lease.web.app.vo.appointment.AppointmentItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {

    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ViewAppointmentMapper viewAppointmentMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Override
    public List<AppointmentItemVo> listItem(Long userId) {
        return viewAppointmentMapper.listItem(userId);
    }

    @Override
    public AppointmentDetailVo getDetailById(Long id) {
        ViewAppointment viewAppointment = viewAppointmentMapper.selectById(id);
        if (ObjectUtils.isEmpty(viewAppointment)){
            throw new LeaseException(ResultCodeEnum.DATA_ERROR);
        }
        Long apartmentId = viewAppointment.getApartmentId();
        ApartmentInfo info = apartmentInfoService.getById(apartmentId);
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoById(apartmentId);
        List<GraphInfo> graphInfoList = graphInfoMapper.selectGraphInfoById(ItemType.APARTMENT, apartmentId);
        BigDecimal roomMinRent = roomInfoMapper.selectRoomMinRentById(apartmentId);
        AppointmentDetailVo appointmentDetailVo = new AppointmentDetailVo();
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(info,apartmentItemVo);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setMinRent(roomMinRent);
        BeanUtils.copyProperties(viewAppointment,appointmentDetailVo);
        appointmentDetailVo.setApartmentItemVo(apartmentItemVo);
        return appointmentDetailVo;
    }
}




